home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Amiga-E / E_v3.2a / Src / Gfx / sim_flock.e < prev    next >
Text File  |  1994-11-08  |  3KB  |  113 lines

  1. -> silly little simulation
  2.  
  3. OPT REG=5
  4.  
  5. MODULE 'intuition/intuition', 'exec/nodes', 'exec/lists', 'tools/constructors'
  6.  
  7. OBJECT creature OF mln
  8.   x,y
  9.   loc:PTR TO LONG
  10. ENDOBJECT
  11.  
  12. DEF creatures=NIL:PTR TO LONG,creaturelist=NIL:PTR TO mlh,num_creatures=100
  13.  
  14. CONST SPACE_XS=64,
  15.       SPACE_YS=64,
  16.       SPACE_X=13,
  17.       SPACE_Y=28,
  18.       XBL=2,
  19.       YBL=2,
  20.       AND_X=$3f,
  21.       AND_Y=$3f
  22.  
  23. CONST XSI=XBL*SPACE_XS,
  24.       YSI=YBL*SPACE_YS
  25.  
  26. PROC main() HANDLE
  27.   DEF w=NIL:PTR TO window,m:PTR TO intuimessage,c=0,a,cc=0
  28.   IF (a:=Val(arg))>1 THEN num_creatures:=a
  29.   IF w:=OpenW(20,20,XSI+42,YSI+42,IDCMP_CLOSEWINDOW OR IDCMP_MOUSEBUTTONS,$F,
  30.          arg,->'BacteriaMania',
  31.          NIL,1,NIL)
  32.     Box(SPACE_X-1,SPACE_Y-1,SPACE_XS*XBL+1+SPACE_X,SPACE_YS*YBL+1+SPACE_Y,1)
  33.     Box(SPACE_X,SPACE_Y,SPACE_XS*XBL+SPACE_X,SPACE_YS*YBL+SPACE_Y,0)
  34.     FOR a:=1 TO num_creatures DO new_creature()
  35.     REPEAT
  36.       move_creatures()
  37.       WHILE m:=GetMsg(w.userport)
  38.         IF (c:=m.class)=IDCMP_MOUSEBUTTONS
  39.         ENDIF
  40.       ENDWHILE
  41.     UNTIL c=IDCMP_CLOSEWINDOW
  42.   ENDIF
  43. EXCEPT DO
  44.   CloseW(w)
  45. ENDPROC
  46.  
  47. PROC move_creatures()
  48.   DEF cr:PTR TO creature,d:PTR TO LONG,x,y,a,b,c,e,aa,bb,nbl=NIL:PTR TO LONG,nb:PTR TO creature,numnb
  49.   cr:=creaturelist.head
  50.   WHILE cr.succ
  51.     a:=0                    -> check were to move
  52.     numnb:=0
  53.     c:=-1
  54.     FOR a:=-1 TO 1
  55.       FOR b:=-1 TO 1
  56.         aa:=a+cr.x; bb:=b+cr.y
  57.         d:=addrxy(aa,bb)
  58.         IF (d[]=NIL) AND (d[]<>cr)
  59.           IF (e:=countcrowd(aa,bb))>=c
  60.           ->IF (e:=countcrowd(d))>=c
  61.             IF IF e=c THEN Rnd(3)=0 ELSE TRUE
  62.               c:=e; x:=aa; y:=bb
  63.             ENDIF
  64.           ENDIF
  65.         ENDIF
  66.       ENDFOR
  67.     ENDFOR
  68.     d:=IF c>=0 THEN addrxy(x,y) ELSE 1
  69.     IF d[]=NIL                    -> if possible, move
  70.       d[]:=cr
  71.       cr.loc[]:=NIL
  72.       cr.loc:=d
  73.       plotxy(x,y,2)
  74.       plotxy(cr.x,cr.y,0)
  75.       cr.x:=x
  76.       cr.y:=y
  77.     ENDIF
  78.     cr:=cr.succ
  79.   ENDWHILE
  80. ENDPROC
  81.  
  82. PROC countcrowd(x,y)
  83.   DEF n=0,a,b,d:PTR TO LONG
  84.   FOR a:=-1 TO 1 DO FOR b:=-1 TO 1 DO IF (d:=addrxy(x+a,y+b)) BUT d[] THEN n++
  85. ENDPROC n
  86.  
  87. PROC new_creature()
  88.   DEF d:PTR TO LONG,a,x,y,cr:PTR TO creature
  89.   IF creatures=NIL
  90.     NEW creatures[SPACE_YS]
  91.     FOR a:=0 TO SPACE_YS-1 DO creatures[a]:=NEW d[SPACE_XS]
  92.     creaturelist:=newlist()
  93.   ENDIF
  94.   REPEAT
  95.     d:=addrxy(x:=Rnd(SPACE_XS),y:=Rnd(SPACE_YS))
  96.   UNTIL d[]=NIL
  97.   d[]:=NEW cr
  98.   AddHead(creaturelist,cr)
  99.   cr.loc:=d
  100.   cr.x:=x
  101.   cr.y:=y
  102.   plotxy(x,y,3)
  103. ENDPROC
  104.  
  105. PROC addrxy(x,y) IS x AND AND_X*SIZEOF LONG+creatures[y AND AND_Y]
  106.  
  107. PROC plotxy(x,y,c)
  108.   DEF xx,yy
  109.   xx:=x AND AND_X*XBL+SPACE_X
  110.   yy:=y AND AND_Y*YBL+SPACE_Y
  111.   IF XBL+YBL=2 THEN Plot(xx,yy,c) ELSE Box(xx,yy,XBL-1+xx,YBL-1+yy,c)
  112. ENDPROC
  113.